package com.moji.controller;


import com.moji.common.base.ApiResult;
import com.moji.common.entity.MyPage;
import com.moji.entity.UploadFiles;
import com.moji.entity.dto.FileDtO;
import com.moji.service.UploadFilesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 文件管理 前端控制器
 * </p>
 *
 * @author mds.
 * @since 2023-04-19
 */
@RestController
@RequestMapping("/web/MjUploadFiles")
@Api(tags = "文件管理", value = "文件管理接口")
public class UploadFilesController {
    //地址类型 oss 或 local
    @Value("${upload.files.type}")
    private String type;

    @Resource
    private UploadFilesService uploadFilesService;

    @PostMapping(value = "/picture")
    @ApiOperation(value = "图片上传(根据配置文件的不同，后端自动校验上传到本地或OSS)")
    public ApiResult uploadPicture(MultipartFile file, HttpServletRequest request) throws IOException {

        List UploadFilesList = new ArrayList();

        try {
          //  for (MultipartFile file1 : file){
                String billTypeCode = String.valueOf(System.currentTimeMillis());
                String uploadFiles = uploadFilesService.uploadPicture(file, "oss");
                UploadFilesList.add(uploadFiles);
          //  }

            return ApiResult.ok(UploadFilesList);
        } catch (Exception e) {
            return ApiResult.failed(e.getMessage());
        }
    }

    @PostMapping(value = "/downDoc")
    @ApiOperation(value = "api文档上传")
    public ApiResult downDoc(MultipartFile file, HttpServletRequest request) throws IOException {

        List UploadFilesList = new ArrayList();

        try {
            //  for (MultipartFile file1 : file){
            String billTypeCode = String.valueOf(System.currentTimeMillis());
            String uploadFiles = uploadFilesService.downDoc(file, "oss");
            UploadFilesList.add(uploadFiles);
            //  }

            return ApiResult.ok(UploadFilesList);
        } catch (Exception e) {
            return ApiResult.failed(e.getMessage());
        }
    }

    /**
     * 查询文件数据
     *
     * @param uploadFiles
     * @return
     */
    @PostMapping("/queryByPage")
    @ApiOperation(value = "全部文件 - 分页查询(只需要传入分页参数)", response = MyPage.class)
    public ApiResult<MyPage<UploadFiles>> queryByPage(@RequestBody FileDtO uploadFiles) {
        MyPage<UploadFiles> myPage = uploadFilesService.queryByPage(uploadFiles);
        return ApiResult.ok(myPage);
    }

    /**
     * 根据商户编码 - 查询供应商活动表
     *
     * @param billNo       单号
     * @param billTypeCode code
     * @return
     */
    @GetMapping("/selFilesByNo")
    @ApiOperation(value = "根据单号和类型查询 上传的附件 - (时间倒叙、不分页)", response = Boolean.class)
    public ApiResult<List> selFilesByNo(String billNo, String billTypeCode) {
        try {
            List<UploadFiles> uploadFiles = uploadFilesService.selFilesByNo(billNo, billTypeCode);
            return ApiResult.ok(uploadFiles);
        } catch (Exception e) {
            return ApiResult.failed(e.getMessage());
        }
    }

    /**
     * 删除客户数据
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteFiles")
    @ApiOperation(value = "删除数据 - 单个删除（一次只允许删除一个）", response = Boolean.class)
    public ApiResult<Boolean> deleteFiles(@RequestParam(value = "id") Long id) {
        try {
            boolean flag = uploadFilesService.deleteFiles(id);
            return ApiResult.ok(flag);
        } catch (Exception e) {
            return ApiResult.failed(e.getMessage());
        }
    }
}

